package gui;

import java.awt.Color;
import java.awt.Graphics2D;

import core.Vector2;

/**
 * A class to display a progress bar in the game.
 * 
 * @author ruben.ortlam
 */
public class Bar {
	private Vector2 pos;
	private Vector2 size;
	
	private int percent;
	
	/**
	 * Creates a new Bar with the given parameters
	 * 
	 * @param pos The position of the new bar.
	 * @param size The size of the new bar.
	 * @param percent The value of the new bar.
	 */
	public Bar(Vector2 pos, Vector2 size, int percent) {
		this.pos = pos;
		this.size = size;
		this.percent = percent;
		
		if(percent > 100)
			percent = 100;
		if(percent < 0)
			percent = 0;
	}
	
	/**
	 * Sets the value of the bar
	 * 
	 * @param percent The new value.
	 */
	public void setPercent(int percent) {
		if(percent > 100 || percent < 0) {
			return;
		}
		
		this.percent = percent;
		return;
	}
	/**
	 * Returns the value of the bar.
	 * 
	 * @return The percentage value of the bar.
	 */
	public int getPercent() {
		return percent;
	}
	
	/**
	 * Draws the bar on the given graphics.
	 * 
	 * @param g The graphics to draw the bar on.
	 */
	public void render(Graphics2D g) {
		int newSize = (int)((size.x - 4) * (percent / 100D));
		int newSize2 = ((int)size.x - 4 - newSize);
		//System.out.println("1: " + newSize + " 2: " + newSize2
		//		+ " percent: " + percent);
		
		Color color = g.getColor();
		
		g.setColor(Color.GRAY);
		g.fillRect((int)pos.x, (int)pos.y, (int)size.x, (int)size.y);
		
		g.setColor(Color.GREEN);
		g.fillRect((int)pos.x + 2, (int)pos.y + 2, 
				(int)newSize, (int)size.y - 4);
		
		g.setColor(Color.RED);
		g.fillRect((int)(pos.x + 2 + newSize), (int)(pos.y + 2), 
				(int)(newSize2), (int)(size.y - 4));
		
//		g.setColor(Color.WHITE);
//		g.drawString("" + percent, (int)(pos.x + size.x / 2D), (int)(pos.y + size.y / 2D));
		
		g.setColor(color);
	}
	
	/**
	 * Returns the position of the bar.
	 * 
	 * @return The position of the bar.
	 */
	public Vector2 getPos() {
		return pos;
	}
	/**
	 * Sets the position of the bar
	 * 
	 * @param pos The new postion.
	 */
	public void setPos(Vector2 pos) {
		this.pos = pos;
	}
}
